using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using CompositeUIExtensions.Winforms;
using Microsoft.Practices.CompositeUI.EventBroker;
using Microsoft.Practices.CompositeUI.Commands;

using LibrErian.Common;
using LibrErian.Common.Constants;
using LibrErian.Model;

namespace LibrErian.Modules.BasicBook
{
    public class ModuleController : WorkItemController
    {
        private Book selectedBook;

        public void Run()
        {
            AddViews();
            InitUI();
            InitState();
        }

        private void InitState()
        {

        }

        private void AddViews()
        {
            SideBarView sideBarView = WorkItem.SmartParts.AddNew<SideBarView>();
            BookListView bookListView = WorkItem.SmartParts.AddNew<BookListView>();

            OutlookBarSmartPartInfo sidebarInfo = new OutlookBarSmartPartInfo();
            sidebarInfo.EventTopicName = "books";
            sidebarInfo.Title = "Books";

            WorkItem.Workspaces[WorkspaceNames.SideBarWorkspace].Show(sideBarView, sidebarInfo);
            WorkItem.Workspaces[WorkspaceNames.ContentWorkspace].Show(bookListView);
        }

        private void InitUI()
        {
            InitMenuStrip();
            InitToolStrip();
        }

        private void InitMenuStrip()
        {
            ToolStripMenuItem miData = new ToolStripMenuItem();
            miData.Name = "dataMenuItem";
            miData.Text = "Data";

            ToolStripMenuItem miGenres = new ToolStripMenuItem();
            miGenres.Name = "genresMenuItem";
            miGenres.Text = "Genres";
            WorkItem.Commands["ManageGenres"].AddInvoker(miGenres, "Click");

            miData.DropDownItems.Add(miGenres);
            WorkItem.UIExtensionSites[UIElementNames.MainMenuStrip].Add(miData);
        }

        private void InitToolStrip()
        {
            ToolStripMenuItem miBook = new ToolStripMenuItem();
            miBook.Name = "bookToolStripMenuItem";
            miBook.Size = new System.Drawing.Size(152, 22);
            miBook.Text = "Book";
            WorkItem.Commands["NewBook"].AddInvoker(miBook, "Click");

            ToolStripMenuItem miAuthor = new ToolStripMenuItem();
            miAuthor.Name = "authorToolStripMenuItem";
            miAuthor.Size = new System.Drawing.Size(152, 22);
            miAuthor.Text = "Author";
            WorkItem.Commands["NewAuthor"].AddInvoker(miAuthor, "Click");

            ToolStripDropDownButton tsdbNew = new ToolStripDropDownButton();
            tsdbNew.Image = Resources.tsbNew_Image;
            tsdbNew.ImageTransparentColor = System.Drawing.Color.Black;
            tsdbNew.Name = "tsbNew";
            tsdbNew.Size = new System.Drawing.Size(48, 22);
            tsdbNew.Text = "New";
            tsdbNew.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { miBook, miAuthor });
            WorkItem.UIExtensionSites[UIElementNames.MainToolStrip].Add(tsdbNew);

            ToolStripButton tsbEdit = new ToolStripButton();
            tsbEdit.Image = Resources.tsbEdit_Image;
            tsbEdit.ImageTransparentColor = System.Drawing.Color.Black;
            tsbEdit.Name = "tsbEdit";
            tsbEdit.Size = new System.Drawing.Size(45, 22);
            tsbEdit.Text = "Edit";
            WorkItem.UIExtensionSites[UIElementNames.MainToolStrip].Add(tsbEdit);
            WorkItem.Commands["EditBook"].AddInvoker(tsbEdit, "Click");
        }

        [EventSubscription("event://SelectedBookChanged")]
        public void CreateNewFile(object sender, EventArgs<Book> args)
        {
            selectedBook = args.Data as Book;
        }

        [CommandHandler("NewBook")]
        public void OnNewFile(object sender, EventArgs e)
        {
            ControlledWorkItem<NewBookController> workItem = WorkItem.WorkItems.AddNew<ControlledWorkItem<NewBookController>>();
            workItem.Controller.Run();
        }

        [CommandHandler("EditBook")]
        public void OnEditFile(object sender, EventArgs e)
        {
            if (selectedBook != null)
            {
                ControlledWorkItem<EditBookController> workItem = WorkItem.WorkItems.AddNew<ControlledWorkItem<EditBookController>>();
                workItem.Controller.Run(selectedBook);
            }
        }

        [CommandHandler("NewAuthor")]
        public void OnNewAuthor(object sender, EventArgs e)
        {
            ControlledWorkItem<NewAuthorController> workItem = WorkItem.WorkItems.AddNew<ControlledWorkItem<NewAuthorController>>();
            workItem.Controller.Run(false);
        }

        [CommandHandler("ManageGenres")]
        public void OnManageGenres(object sender, EventArgs e)
        {
            ControlledWorkItem<ManageGenreController> workItem = WorkItem.WorkItems.AddNew<ControlledWorkItem<ManageGenreController>>();
            workItem.Controller.Run();
        }
    }
}
